import { Module } from 'vuex';
import { RootState } from '../root.state';

const initToken: string = window.sessionStorage.getItem('token') as string;

console.log('initToken', initToken);

export interface UserState {
  token?: string ;
  name?: string;
}

const getters = {};
const actions = {
  login: (context: any, token: string) => {
    context.commit('changeToken', token);
  },
};

const mutations = {
  changeToken: (state: UserState, token: string) => {
    console.log('token', token);
    state.token = token;
  },
};

const initState: UserState = {
  token: initToken == null ? undefined : initToken,
  name: undefined,
};

const user: Module<UserState, RootState> = {
  namespaced: true,
  state: initState,
  getters,
  actions,
  mutations,
};

export default user;
